// pages/myhomepage/myhomepage.js
const app = getApp()
const qiniu = require("../../utils/qiniu.min.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifNotice:false,
    userId:'',
    userInfo:{},
    newName:'',
    newCompany:'',
    newPosition:'',
    newCompanyAddress:'',
    newPhone:'',
    isOneSelf:true,
    viewUid:'',
    user:{},
    isClick:false,

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options.viewUid)
    if (options.viewUid == undefined){
      that.setData({
        isOneSelf:true
      })
    }else{
      that.setData({
        isOneSelf: false,
        viewUid: options.viewUid
      })
    }
    console.log("id",that.data.viewUid)
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getUserInfo: function() {
    var that = this;
    var data = {}
    var URL= ''
    if(!that.data.isOneSelf){
      // data ={
      //   "viewUid": that.data.viewUid,
      // }
      URL = "wechat/user/getOtherInformation?&viewUid=" + that.data.viewUid
    }else{
      URL = "wechat/user/getUserInformation"
    }
    app.req({
      url: URL,
      method: "post",
      // data: data,
      success: function(res) {
        if (200 == res.code) {
          that.setData({
            userInfo:res.data,
            newName: res.data.realName,
            newCompany: res.data.company,
            newPosition: res.data.post,
            newCompanyAddress: res.data.companyAddress,
            newPhone: res.data.telphone
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {   
    var that = this
    app.getUserInfo(function (userInfo) {
      that.setData({
        userId: userInfo.id, 
        userInfo:userInfo      
      });
    });
    console.log(that.data.userInfo)
    that.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  changeAvatar:function(){
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        const filepath = res.tempFilePaths[0]
        const src = filepath
        console.log("原地址", src)
        wx.navigateTo({
          url: `../upload/upload?src=${src}`
        })
      }
    })

    
    





    //   },
    //   fail: function () {
    //     // fail
    //     wx.showToast({
    //       title: res.errMsg,
    //       icon: 'none',
    //       duration: 2000
    //     })
    //   },
    //   complete: function () {
    //     // complete
    //   }
    // })
  },
  ifNotice(){
    this.setData({
      ifNotice:true
    })
  },
  topicInput: function (option){
    var that = this
    var name = option.currentTarget.dataset.title
    var value = option.detail.value
    if (value.trim().length == 0 ){
      console.log('都为空')
      value = ''
      if (name == 'newName') {
        that.setData({
          newName: value
        })
      } else if (name == 'newCompany') {
        that.setData({
          newCompany: value
        })
      } else if (name == 'newPosition') {
        that.setData({
          newPosition: value
        })
      } else if (name == 'newCompanyAddress') {
        that.setData({
          newCompanyAddress: value
          // ifNotice: false
        })
        console.log('dizhi', that.data.newCompanyAddress)
      }
    }else{
    if (name == 'newName') {
      that.setData({
        newName: value
      })
    } else if (name == 'newCompany') {
      that.setData({
        newCompany: value
      })
    } else if (name == 'newPosition') {
      that.setData({
        newPosition: value
      })
    } else if (name == 'newCompanyAddress') {
      that.setData({
        newCompanyAddress: value
        // ifNotice: false
      })
      console.log('dizhi', that.data.newCompanyAddress)
    } else if (name == 'newPhone') {

      that.setData({
        newPhone: value
      })
    }
    }
  },
  topicblur: function (option){
    var that= this
    var name = option.currentTarget.dataset.title
    var value = option.detail.value
    // console.log("地址",value)
    if(name == 'newName'){
      that.setData({
        newName:value
      })
    } else if (name == 'newCompany'){
      that.setData({
        newCompany: value
      })
    } else if (name == 'newPosition') {
      that.setData({
        newPosition: value
      })
    } else if (name == 'newCompanyAddress') {
      that.setData({
        newCompanyAddress: value,
        ifNotice:false
      })
      console.log('dizhi', that.data.newCompanyAddress)
    } else if (name == 'newPhone') {
      
        that.setData({
          newPhone: value
        })
      
      
    }



  },
  bottomSave: function(){
    var that = this
    if(that.data.isClick) {
      return
    }
    console.log('dizhichuan',that.data.newCompanyAddress)
   
    var data = {
      realName: that.data.newName,
      company: that.data.newCompany,
      post: that.data.newPosition,
      companyAddress: that.data.newCompanyAddress,
      telphone: that.data.newPhone,
    }
    that.setData({
      isClick:true
    })
    if (that.data.newName==''){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        isClick: false
      })
      return;
    }else if (!(/^1[3456789]\d{9}$/.test(that.data.newPhone))) {
      wx.showToast({
        title: '请输入正确的手机号格式',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        isClick: false
      })
      return;
    } else {
      // console.log('个人资料保存',data)
      // that.toggle("userInfo.realName", that.data.newName);
      // that.toggle("userInfo.company", that.data.newCompany);
      // that.toggle("userInfo.post", that.data.newPosition);
      // that.toggle("userInfo.companyAddress", that.data.newCompanyAddress);
      // that.toggle("userInfo.telphone", that.data.newPhone);
      // that.toggle("userInfo.avatarUrl", that.data.userInfo.avatarUrl);
      app.getUserInfo(function (userInfo) {
        app.req({
          url: "wechat/user/updateUserInformation",
          method: "post",
          data: data,
          success: function (res) {
            if (200 == res.code) {
              wx.showToast({
                title: '保存成功',
                icon: 'none',
                duration: 2000
              })
              that.setData({
                isClick: false
              })
              that.getUserInfo()
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
              that.setData({
                isClick: false
              })
            }
          }
        });
      });

    }
    
  },
  toggle: function (type, val) {
    this.setData({
      [type]: val
    });
  },
  
})